﻿using EagleProject.LinQDataContext;
using EagleProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Script.Serialization;

namespace EagleProject.Controllers
{
    public class LGAController : ApiController
    {
        // GET api/lga/
        public HttpResponseMessage Get()
        {
            var data = DatabaseController.GetLGAs();
            LocalGovAreaModel[] rData = new LocalGovAreaModel[data.Count];
            for (int i = 0; i < data.Count; i++)
            {
                var item = data[i];
                rData[i] = new LocalGovAreaModel()
                {
                    ID = item.ID,
                    Name = item.Name
                };
            }
            var json = new JavaScriptSerializer().Serialize(rData);
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StringContent(json);
            return response;
        }

        // GET api/lga/5
        public HttpResponseMessage Get(int id)
        {
            var data = DatabaseController.GetLGAsByState(id);
            LocalGovAreaModel[] rData = new LocalGovAreaModel[data.Count];
            for (int i = 0; i < data.Count; i++)
            {
                var item = data[i];
                rData[i] = new LocalGovAreaModel()
                {
                    ID = item.ID,
                    Name = item.Name
                };
            }
            var json = new JavaScriptSerializer().Serialize(rData);
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StringContent(json);
            return response;
        }
    }
}
